import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		token:'',
		userId:'',
		openId: '',
		sessionKey:'',
		mobile:'',
		userinfo:{},
		userInfoFromWx:{}
	},
	getters:{
		
	},
	mutations: {
		init(state){
			state.hasLogin=uni.getStorageSync('hasLogin');
			state.token=uni.getStorageSync('token');
			state.userId=uni.getStorageSync('userId');
		},
		login(state,data) {
			state.hasLogin = true;
			state.token=data.token;
			state.userId=data.userId;
			uni.setStorageSync('hasLogin',true);
			uni.setStorageSync('token',data.token);
			uni.setStorageSync('userId',data.userId);
			
		},
		setOpenidSessionkey(state, data) {
			state.openId = data.openId
			state.sessionKey = data.sessionKey
		},
		setUserInfoFromWx(state,userInfoFromWx){
			state.userInfoFromWx=userInfoFromWx;
		},
		logout(state) {
			state.hasLogin = false
			state.token=''
			state.userId=''
			uni.setStorageSync('hasLogin',false);
			uni.setStorageSync('token','');
			uni.setStorageSync('userId','');
		}
	},
    
	actions: {
		init(context){
			context.commit('init')
		},
		login(context,data){
			context.commit('login',data)
		},
		setOpenidSessionkey(context, data) {
			context.commit('setOpenidSessionkey', data)
		},
		setUserInfoFromWx(context,userInfoFromWx){
			context.commit('setUserInfoFromWx',userInfoFromWx)
		},
		logout(context){
			context.commit('logout')
		}
	}
})

export default store